package me.zhengjie.minbearwcs.aop;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.utils.JsonUtil;
import me.zhengjie.minbearwcs.utils.LogFileKey;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Aspect
@Component
public class WebAop {


    @Pointcut(
            "execution(public * me.zhengjie.minbearwcs.controller..*(..)) && " +
                    "!execution(public * me.zhengjie.minbearwcs.controller.common.MinBearUploadController.*(..)) &&" +
                    "!execution(public * me.zhengjie.minbearwcs.controller.agv..*(..))"
    )
    public void webControllerPointCut() {
    }

    @Pointcut(
            "execution(public * me.zhengjie.minbearwcs.controller.common.MinBearUploadController.*(..))"
    )
    public void webUploadPointCut() {
    }


    @Pointcut(
            "execution(public * me.zhengjie.minbearwcs.controller.agv..*(..))"
    )
    public void agvInfoPointCut() {
    }


    @Around("webControllerPointCut()")
    public Object webInfoParamIn(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String method = request.getMethod();
        String ip = request.getRemoteAddr() + ":" + request.getRemotePort();
        String uri = request.getRequestURI();

        MDC.put(LogFileKey.KEY, LogFileKey.Module.WEB);
        log.info("主机: {},方法: {},URI: {},入参：{}", ip, method, uri, JsonUtil.getJsonInfo(point.getArgs()));

        Object proceed = point.proceed();

        log.info("主机: {},方法: {},URI: {},出参: {}", ip, method, uri, JsonUtil.getJsonInfo(proceed));
        MDC.remove(LogFileKey.KEY);
        return proceed;
    }


    @Around("webUploadPointCut()")
    public Object uploadInfoParamIn(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String method = request.getMethod();
        String ip = request.getRemoteAddr() + ":" + request.getRemotePort();
        String uri = request.getRequestURI();

        MDC.put(LogFileKey.KEY, LogFileKey.Module.WEB);
        log.info("主机: {},方法: {},URI: {},入参：{}", ip, method, uri, point.getArgs());

        Object proceed = point.proceed();

        log.info("主机: {},方法: {},URI: {},出参: {}", ip, method, uri, JsonUtil.getJsonInfo(proceed));
        MDC.remove(LogFileKey.KEY);
        return proceed;
    }


    @Around("agvInfoPointCut()")
    public Object agvInfoParamIn(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String method = request.getMethod();
        String ip = request.getRemoteAddr() + ":" + request.getRemotePort();
        String uri = request.getRequestURI();

        MDC.put(LogFileKey.KEY, LogFileKey.Module.AGV);
        log.info("主机: {},方法: {},URI: {},入参：{}", ip, method, uri, point.getArgs());

        Object proceed = point.proceed();

        log.info("主机: {},方法: {},URI: {},出参: {}", ip, method, uri, JsonUtil.getJsonInfo(proceed));
        MDC.remove(LogFileKey.KEY);
        return proceed;
    }

}
